<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fetch + Vue test</title>

  <script type="text/javascript" src="../_js/vue.min.js"></script>

  <style>
    #log{
      height: 200px;
      width: 400px;
      margin-top: 10px;
      /* border-color: #f70; */
    }
  </style>

</head>
<body>
  <div id="root">
    <h2>Vue + Fetch test -- {{message}}</h2>

    <button @click="doFetchGet">Fetch-Get</button>
    <button @click="doFetchPost">Fetch-Post</button>
    <br>
    <textarea id="log" ref="log">Logging Message</textarea>

  </div>
  
</body>

<script type="text/javascript">

  Vue.config.productionTip = false

  const vm = new Vue({
    el: '#root',
    data: {
      message: 'Are you OK?',
      emps: [
        {id: 1, name: 'Jack', age: 18},
        {id: 2, name: 'Rose', age: 22},
        {id: 3, name: 'White', age: 11}
      ],
      point: {
        x: 0,
        y: 0
      }
    },
    methods: {
      doFetchGet() {
        fetch('https://jsonplaceholder.typicode.com/todos/10')
          .then(response => response.json())
          .then(json => {
            this.$refs.log.value = JSON.stringify(json)
            console.log(json)
          })
      },
      doFetchPost() {
        fetch('https://jsonplaceholder.typicode.com/posts', {
          method: 'POST',
          body: JSON.stringify({
            title: 'Test',
            other: 'Other',
            tag: 9
          }),
          headers: {
            'content-type': 'application/json; charset=UTF-8',
            'customName': '###json test###'
          }
        })
        .then(response => response.json())
        .then(json => {
          this.$refs.log.value = JSON.stringify(json)
          console.log(json)
        })
      },
    }
  })

</script>

</html>